﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using webnew.Common;
namespace webnew.Systems
{
    public partial class S_roleEdit : WebPageBase.EditBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //设置列表页面名，即 返回的页面名
            _ListSrc = "S_roleList.aspx";
            if (!IsPostBack)
            {
                //设置页面标题
                SetTitle(true, "后台管理 >> 角色管理");
            }

        }
        /// <summary>
        /// 重写基类，显示数据方法
        /// </summary>
        protected override void ShowData()
        {
            
            Model.Sys_RoleInfo model = new BLL.Sys_Role().GetList(_RecordId);
            txtRoleName.Text = model.rolename;
            txtRemark.Text = model.remark;
            ViewState["currentRoleName"] = model.rolename; //保存当前角色名称,用于验证判断
        }

        /// <summary>
        /// 重写基类，验证数据方法
        /// </summary>
        protected override List<PageValid> _SetValidData()
        {
            List<PageValid> lst = new List<PageValid>();
            lst.Add(new PageValid("角色名称", txtRoleName.Text, PageValid.validType.CommonValid, false, 25));
            lst.Add(new PageValid("序号", txtRemark.Text, PageValid.validType.CommonValid, false, 20));
            lst.Add(new PageValid("序号", txtRemark.Text, PageValid.validType.IntValid, "请输入正确的数值"));
            //判断是否存在相同的角色名称
            if (_RecordId != null) //修改
            {
                //判断用户在修改状态下是否修改过角色名称,没修改过 则跳过验证
                if (ViewState["currentRoleName"].ToString() != txtRoleName.Text)
                    lst.Add(new PageValid("角色名称", "1", new BLL.Sys_Role().ExistRolename(txtRoleName.Text), PageValid.validType.NoSameValid, "已存在,请重新输入"));
            }
            else //添加
                lst.Add(new PageValid("角色名称", "1", new BLL.Sys_Role().ExistRolename(txtRoleName.Text), PageValid.validType.NoSameValid, "已存在,请重新输入"));

            return lst;
           
        }

        /// <summary>
        /// 重写基类保存数据方法
        /// </summary>
        protected override int SaveData()
        {
            Model.Sys_RoleInfo model = new Model.Sys_RoleInfo();
            model.Id = _RecordId;
            model.rolename = txtRoleName.Text;

            model.remark = txtRemark.Text;
            model.isdel = 0;

            //----------系统日志model

            Model.Sys_LogInfo modelLog = new webnew.Model.Sys_LogInfo();
            modelLog.UserID = UserInfor.Id;                  //用户ID
            modelLog.IP = new BLL.Sys_Log().GetUserIP(System.Web.HttpContext.Current.Request);  //IP
            modelLog.Module = new BLL.Sys_Navigation().GetModuleName(this.GetType().BaseType.Name);   //模块名
            modelLog.CreateDate = DateTime.Now;           //创建时间

            string error = "";                   //获取错误信息

            if (_RecordId != null)  //修改
            {
                try
                {
                    return new BLL.Sys_Role().Update(model);
                }
                catch (Exception e)
                {
                    error = e.ToString();
                }
                finally
                {
                    //添加一条系统日志
                    new BLL.Sys_Log().AddSysLog(error, "修改", modelLog, "系统角色", "角色，角色名称为" + txtRoleName.Text);
                }
            }
           
            else                //添加
            { 
                try
                {   
                    return new BLL.Sys_Role().Add(model);
                }
                catch (Exception e)
                {
                    error = e.ToString();
                }
                finally
                {
                    //添加一条系统日志
                    new BLL.Sys_Log().AddSysLog(error, "添加", modelLog, "系统角色", "角色，角色名称为" + txtRoleName.Text);
                }
            }

            //弹出文本框成功或者失败
            if (error == "") { return 1; }       //成功
            else { return 0; }                   //失败
            
        }
    }
}
